package com.veo.controller.admin;

import com.veo.entity.Blog;
import com.veo.entity.BlogQuery;
import com.veo.entity.Type;
import com.veo.entity.User;
import com.veo.service.BlogService;
import com.veo.service.TagService;
import com.veo.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/admin")
public class BlogController {

    private static final String INPUT = "admin/blogs-input";
    private static final String LIST = "admin/blogs";
    private static final String REDIRECT_LIST = "redirect:/admin/blogs";

    @Autowired
    private BlogService blogService;

    @Autowired
    TypeService typeService;

    @Autowired
    TagService tagService;

    //分页查询，每页2条记录，按更新时间倒序排序，将查询条件封装到Blog对象中
    @GetMapping("/blogs")
    public String blogs(@PageableDefault(size = 2, sort = {"updatedTime"}, direction = Sort.Direction.DESC) Pageable pageable, BlogQuery blog, Model model){
        //查询类型
        model.addAttribute("types",typeService.listType());
        model.addAttribute("page",blogService.listBlog(pageable,blog));
        return LIST;
    }

    //分页查询，每页2条记录，按更新时间倒序排序，将查询条件封装到Blog对象中
    @PostMapping("/blogs/search")
    public String search(@PageableDefault(size = 2, sort = {"updatedTime"}, direction = Sort.Direction.DESC) Pageable pageable, BlogQuery blog, Model model){
        model.addAttribute("page",blogService.listBlog(pageable,blog));
        //使用thymeleaf来局部渲染片段blogList,渲染页面的部分
        return "admin/blogs :: blogList";
    }

    //跳转到博客新增页面
    @GetMapping("/blogs/input")
    public String input(Model model){

        //初始化分类
        model.addAttribute("types",typeService.listType());
        //初始化标签页
        model.addAttribute("tags",tagService.listTag());
        //修改，添加公用一个页面，初始化一个blog
        model.addAttribute("blog",new Blog());

        return INPUT;
    }


    //添加保存和修改博客的控制层
    @PostMapping("/blogs")
    public String post(Blog blog, HttpSession session, RedirectAttributes attributes){
        //设置博客的作者
        blog.setUser((User) session.getAttribute("user"));
        //根据前端页面传递的id查询类型的id
        blog.setType(typeService.getType(blog.getType().getId()));

        //根据前端传递的标签id组，查询该博客所属的标签
        blog.setTags(tagService.listTag(blog.getTagIds()));

        Blog b;
        //有id为添加，否则为修改，修改时查询所有字段后在修改
        if (blog.getId() == null){
            b = blogService.saveBlog(blog);
        }else {
            b = blogService.updateBlog(blog.getId(),blog);
        }

        if (b == null){
            //分类添加失败，返回失败信息
            attributes.addFlashAttribute("msg","操作失败");
        }else {
            //分类添加成功，返回成功信息
            attributes.addFlashAttribute("msg","操作成功");
        }
        return REDIRECT_LIST;
    }

    //跳转修改页面
    @GetMapping("/blogs/{id}/input")
    public String editInput(@PathVariable(name = "id") Long id, Model model){

        //初始化分类
        model.addAttribute("types",typeService.listType());
        //初始化标签页
        model.addAttribute("tags",tagService.listTag());

        Blog blog = blogService.getBlog(id);
        //处理tagId为字符串
        blog.init();
        System.out.println(blog);
        //修改，添加公用一个页面，初始化一个blog
        model.addAttribute("blog",blog);

        return INPUT;
    }

    //博客删除
    @GetMapping("/blogs/{id}/delete")
    public String deleteBlog(@PathVariable(name = "id") Long id,RedirectAttributes attributes){
        blogService.deleteBlog(id);
        attributes.addFlashAttribute("msg","删除成功");
        return REDIRECT_LIST;
    }



}
